﻿using Castle.Windsor;
using Castle.Windsor.Installer;
using Pe.Petecos.Comercial.Aplicacion.Core.Factory;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace Pe.Petecos.Comercial.Presentacion
{
    // Nota: para obtener instrucciones sobre cómo habilitar el modo clásico de IIS6 o IIS7, 
    // visite http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static IWindsorContainer container;

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            BootstrapContainer();
        }
        protected void Application_End()
        {
            container.Dispose();
        }

        private static void BootstrapContainer()
        {
            //Database.SetInitializer<PoliticaDbContextProvider>(new DbContextInitializer());
            //Database.SetInitializer<ApplicationAuditDbContextProvider>(new CreateDatabaseIfNotExists<ApplicationAuditDbContextProvider>());
            container = new WindsorContainer()
                .Install(FromAssembly.InThisApplication());
            var controllerFactory = new WindsorControllerFactory(container.Kernel);
            ControllerBuilder.Current.SetControllerFactory(controllerFactory);
        }
    }
}